﻿using System;
using System.Collections.Generic;
using System.Linq;

using FineUI;
using ZHDJ.JoinParty.BLL;
using System.Data;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.Core.Util;

namespace JoinParty.admin.FlowStep
{
    public partial class JoinPartyInspection : PageBase
    {
        public string ID
        {
            get
            {
                return GetQueryIntValue("id");
            }
        }
        /// <summary>
        /// 思想汇报
        /// </summary>
        public DataSet ThinkDataSource
        {
            get
            {
                return PartyJoinThoughtReport.GetGridData(this.CurrentStandID);
            }
        }

        /// <summary>
        /// 考察
        /// </summary>
        public DataSet OptionDataSource
        {
            get
            {
                return PartyJoinReviewOption.GetGridData(this.CurrentStandID); ;
            }
        }

        public string CultureMeasuresContent { get; set; }
        public string BranchOpinionContent { get; set; }
        public string RemarkContent { get; set; }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }
        private void BindData()
        {
            //思想汇报新增
            string addThinkUrl = "JoinPartyInspectionTEdit.aspx?StandID=" + this.CurrentStandID;
            this.btnAddThink.OnClientClick = this.WAddThink.GetShowReference(addThinkUrl);

            this.lbtOne.OnClientClick = WPartyGroup.GetShowReference("JoinPartyInspectionOEdit.aspx?OrderNo=1&StandID=" + this.CurrentStandID, "半年考察情况");
            this.lbtTwo.OnClientClick = WPartyGroup.GetShowReference("JoinPartyInspectionOEdit.aspx?OrderNo=2&StandID=" + this.CurrentStandID, "半年考察情况");
            this.lbtThree.OnClientClick = WPartyGroup.GetShowReference("JoinPartyInspectionOEdit.aspx?OrderNo=3&StandID=" + this.CurrentStandID, "半年考察情况");
            this.lbtFour.OnClientClick = WPartyGroup.GetShowReference("JoinPartyInspectionOEdit.aspx?OrderNo=4&StandID=" + this.CurrentStandID, "半年考察情况");
            this.lbtFive.OnClientClick = WPartyGroup.GetShowReference("JoinPartyInspectionOEdit.aspx?OrderNo=5&StandID=" + this.CurrentStandID, "半年考察情况");
            this.lbtSix.OnClientClick = WPartyGroup.GetShowReference("JoinPartyInspectionOEdit.aspx?OrderNo=6&StandID=" + this.CurrentStandID, "半年考察情况");

            var example = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            PartyJoinApplycation app = ProcesUtil.GetApplycation(example.AppllicationID);
            this.hdAppID.Value = app.AppllicationID;
            this.hdAppLoginID.Value = app.LoginID;
            this.hdCurrentStandID.Value = this.CurrentStandID;

            if (this.CurrentStepID == ProcesUtil.JoinStep31)
            {
                this.lbtFour.Hidden = this.lbtFive.Hidden = this.lbtSix.Hidden = this.lbtThree.Hidden = true;
            }

            //页面只读显示
            if (base.ReadOnly == "1")
            {
                this.btnSubmit.Hidden = true;
                if (IsAdmin)
                    this.btnSaveClose.Hidden = false;
                else
                    this.btnSaveClose.Hidden = true;
            }
            hdReadOnly.Value = base.ReadOnly;
        }

        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            this.GridThink.DataSource = this.ThinkDataSource;
            this.GridThink.DataBind();
            var rep = DB.PartyJoinReviewOptions.Where(a => a.StandID == this.CurrentStandID).FirstOrDefault();
            if (rep == null)
            {
                rep = new PartyJoinReviewOption();
                rep.ID = StringHelper.NewID;
                rep.LoginID = this.hdAppLoginID.Value;
                rep.AppllicationID = this.hdAppLoginID.Value;
                rep.StandID = this.CurrentStandID;
                DB.PartyJoinReviewOptions.Add(rep);
                DB.SaveChanges();
            }
            else
            {
                this.CultureMeasuresContent = rep.CultureMeasures;
                this.BranchOpinionContent = rep.BranchOpinion;
                this.RemarkContent = rep.Remark;
            }
        }



        private void SaveData(bool isSubmit)
        {
            var rep = DB.PartyJoinReviewOptions.Where(a => a.StandID == this.CurrentStandID).FirstOrDefault();
            rep.BranchOpinion = this.Request.Form["editorBranchOpinion"];
            rep.CultureMeasures = this.Request.Form["editorCultureMeasures"];
            rep.AppllicationID = this.AppllicationID;
            rep.StepID = this.CurrentStepID;
            rep.Remark = this.Request.Form["editorRemark"];
            DB.SaveChanges();

            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过
            var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            oModel.HandleUserID = this.CurrentUser.LoginID;
            oModel.HandleUserName = this.CurrentUser.UserName;
            oModel.HandleTime = DateTime.Now;
            oModel.Status = (int)Enum_FlowStanceStatus.通过;

            bool Result = false;
            if (isSubmit)
            {
                Result = ProcesUtil.ApprovalProcesStep(oModel, out Message);
                Alert.Show("提交成功！", String.Empty, ActiveWindow.GetHideReference());
            }
            else
                Alert.Show("保存成功！", String.Empty, ActiveWindow.GetHideReference());
            if (Result)
            {
                if (Message != "")
                {
                    Alert.Show(Message, String.Empty, ActiveWindow.GetHideReference());
                }
            }
        }

        #endregion

        #region
        public bool CheckValue()
        {
            bool ret = true;
            //判断思想汇报是否已填写，领导考核意见是否已填写 Step8 and step25
            if (this.ThinkDataSource.Tables[0].Rows.Count == 0)
            {
                Alert.Show("请填写思想汇报，再提交！");
                ret = false;
            }

            return ret;
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckValue())
            {
                this.SaveData(true);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            this.SaveData(false);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridThink_RowCommand(object sender, GridCommandEventArgs e)
        {
            object[] values = GridThink.DataKeys[e.RowIndex];
            string ID = Convert.ToString(values[0]);
            if (e.CommandName == "Delete")
            {
                var del = DB.PartyJoinThoughtReports.FirstOrDefault(t => t.ID == ID);
                DB.PartyJoinThoughtReports.Remove(del);
                DB.SaveChanges();
            }
            LoadData();
        }

        protected void WRelation_Close(object sender, WindowCloseEventArgs e)
        {
            LoadData();
        }

        protected void WAddThink_Close(object sender, WindowCloseEventArgs e)
        {
            LoadData();
        }
    }
}